package com.hzhr.iot.meterplan.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.meterplan.constant.Meterplan_ErrorsEnum;
import com.hzhr.iot.meterplan.dao.mapper.generator.RuhujihuaEntityMapper;
import com.hzhr.iot.meterplan.dao.mapper.generator.custom.RuhujihuaEntityCustomMapper;
import com.hzhr.iot.meterplan.entity.RuhujihuaEntity;
import com.hzhr.iot.meterplan.entity.ext.RuhujihuaEntityExt;
import com.hzhr.iot.meterplan.model.meterPlan.MeterPlanGetModel;
import com.hzhr.iot.meterplan.model.meterPlan.MeterPlanModel;
import com.hzhr.iot.meterplan.model.meterPlan.MeterPlanPostModel;
import com.hzhr.iot.meterplan.model.meterPlan.MeterPlanPutModel;
import com.hzhr.iot.meterplan.service.base.BaseService;
import com.hzhr.iot.meterplan.util.StrUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@CygService(entity = RuhujihuaEntity.class, model = MeterPlanModel.class)
public class MeterPlanService extends BaseService {

    @Autowired
    RuhujihuaEntityCustomMapper ruhujihuaEntityCustomMapper;

    @Autowired
    RuhujihuaEntityMapper ruhujihuaEntityMapper;

    //region javadoc注释 : get
    /**
     * 查询登录用户管辖范围下的远传表抄表计划 <br/><br/>
     * 1. 查询抄表计划：查询登录用户管辖范围下的远传表抄表计划(按抄表计划编号降序排列) <br/>
     * 2. 返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _equal_id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：where MeterPlanModel.id = #{_equal_id};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_des : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：where MeterPlanModel.des like % + #{_like_des} + %;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_typeId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：where MeterPlanModel.typeId = #{_equal_typeId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_statusId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：where MeterPlanModel.statusId = #{_equal_statusId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @throws ServiceException <br/>
     * @see MeterPlanGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询登录用户管辖范围下的远传表抄表计划
    //1. 查询抄表计划：查询登录用户管辖范围下的远传表抄表计划(按抄表计划编号降序排列)
    //2. 返回信息
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件：where MeterPlanModel.id = #{equal_id}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "like_des", value = "筛选条件：where MeterPlanModel.des like % + #{like_des} + %", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_typeId", value = "筛选条件：where MeterPlanModel.typeId = #{equal_typeId}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_statusId", value = "筛选条件：where MeterPlanModel.statusId = #{equal_statusId}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_equal_id", value = "筛选条件：where MeterPlanModel.id = #{_equal_id}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_like_des", value = "筛选条件：where MeterPlanModel.des like % + #{_like_des} + %", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_typeId", value = "筛选条件：where MeterPlanModel.typeId = #{_equal_typeId}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_statusId", value = "筛选条件：where MeterPlanModel.statusId = #{_equal_statusId}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/meter-plans", method = RequestMethod.GET)
    //endregion
    public MeterPlanGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        /* 查询抄表计划：查询登录用户管辖范围下的远传表抄表计划(按抄表计划编号降序排列) */
        UserCardModel userCard = getUserCard(header);
        String orgId = userCard.getUserCode().replaceFirst("010009", "00").replaceFirst("01", "00");
        if (!StringUtils.isEmpty(orgId)) {
            orgId = orgId + "%";
        }
        String id = HzhrTypeUtil.object2string(query.get("_equal_id"));
        String des = HzhrTypeUtil.object2string(query.get("_like_des"));
        if (!StringUtils.isEmpty(des)) {
            des = "%"+ des + "%";
        }
        String typeId = HzhrTypeUtil.object2string(query.get("_equal_typeId"));
        String statusId = HzhrTypeUtil.object2string(query.get("_equal_statusId"));

        String _show = HzhrTypeUtil.object2string(query.get("_show"));
        Boolean _show_total = HzhrTypeUtil.object2boolean(query.get("_show_total"));

        Integer _page = HzhrTypeUtil.object2integer(query.get("_page"));
        Integer _count = HzhrTypeUtil.object2integer(query.get("_count"));
        Integer start = null;
        if (_page!= null){
            start = (_page * _count)-_count +1;
        }
        _count = _page * _count;

        List<RuhujihuaEntityExt> ruhujihua_1 = ruhujihuaEntityCustomMapper.selectCbjhAndCbjh(start,_count,id,des,typeId,statusId,orgId);

        Integer Total = ruhujihuaEntityCustomMapper.selectCbjhAndCbjhCount(id,des,typeId,statusId,orgId);

        /* 返回信息 */
        MeterPlanGetModel result = new MeterPlanGetModel();
        if ("one".equals(_show)) {
            if (ruhujihua_1 != null && ruhujihua_1.size() > 0) {
                RuhujihuaEntityExt ruhujihua_2 = ruhujihua_1.get(0);
                MeterPlanModel meterPlanModel = new MeterPlanModel();
                meterPlanModel.setId(ruhujihua_2.getRhId());
                meterPlanModel.setDes(HzhrTypeUtil.object2string(ruhujihua_2.getRhJhsm()));  //有重复；需注意
                meterPlanModel.setTypeId(ruhujihua_2.getRhRhlxId()); //类型编号
                meterPlanModel.setTypeDes(ruhujihua_2.getTYPEDESC()); //类型描述 TypeDess
                meterPlanModel.setStatusId(ruhujihua_2.getRhZhuangtai()); //状态编号
                //1执行中 2待执行 3暂停
                meterPlanModel.setStatusDes(ruhujihua_2.getZTDES());// 状态描述  ZTDES
                meterPlanModel.setCollectFrequencyId(ruhujihua_2.getCfId()); //采集频率编号
                //根据采集频率ID，查询出采集频率描述
                meterPlanModel.setCollectFrequencyDes(ruhujihua_2.getCFNAMEDES()); //采集频率描述 CFNAMEDES
                meterPlanModel.setAccountPeriodId(ruhujihua_2.getRhJhzq()); //计费周期编号
                meterPlanModel.setAccountPeriodDes(ruhujihua_2.getZQDES());//计费周期描述 ZQDES
                meterPlanModel.setPlanPeriodId(ruhujihua_2.getRhYflx());  //计划周期编号
                meterPlanModel.setPlanPeriodDes(ruhujihua_2.getPCZTDES());//计划周期描述 计划周期描述
                meterPlanModel.setAccountFrequency(ruhujihua_2.getRhJhpl().intValue());//计费频率  ruhujihua_2.getRhJhpl()
                meterPlanModel.setAccountDay(HzhrTypeUtil.string2integer(ruhujihua_2.getRhJhrq()));
                meterPlanModel.setVeeCheckId(ruhujihua_2.getVeeId());
                //vee校验描述
                meterPlanModel.setVeeCheckDes(ruhujihua_2.getVEEDES()); //vee校验描述 VEEDES
                result.setModel(meterPlanModel);
            }
        }
        if ("list".equals(_show)) {
            List<MeterPlanModel> meterPlanModels = new ArrayList<>();
            if (ruhujihua_1 != null) {
                for (RuhujihuaEntityExt ruhujihuaEntityExt : ruhujihua_1) {
                    MeterPlanModel meterPlanModel = new MeterPlanModel();
                    meterPlanModel.setId(ruhujihuaEntityExt.getRhId());
                    meterPlanModel.setDes(HzhrTypeUtil.object2string(ruhujihuaEntityExt.getRhJhsm()));
                    meterPlanModel.setTypeId(ruhujihuaEntityExt.getRhRhlxId()); //类型编号
                    meterPlanModel.setTypeDes(ruhujihuaEntityExt.getTYPEDESC()); //类型描述 TypeDess
                    meterPlanModel.setStatusId(ruhujihuaEntityExt.getRhZhuangtai()); //状态编号
                    //1执行中 2待执行 3暂停
                    meterPlanModel.setStatusDes(ruhujihuaEntityExt.getZTDES());// 状态描述
                    meterPlanModel.setCollectFrequencyId(ruhujihuaEntityExt.getCfId()); //采集频率编号
                    //根据采集频率ID，查询出采集频率描述
                    meterPlanModel.setCollectFrequencyDes(ruhujihuaEntityExt.getCFNAMEDES()); //采集频率描述 CFNAMEDES
                    meterPlanModel.setAccountPeriodId(ruhujihuaEntityExt.getRhJhzq()); //计费周期编号
                    meterPlanModel.setAccountPeriodDes(ruhujihuaEntityExt.getZQDES());//计费周期描述 ZQDES
                    meterPlanModel.setPlanPeriodId(ruhujihuaEntityExt.getRhYflx());  //计划周期编号
                    meterPlanModel.setPlanPeriodDes(ruhujihuaEntityExt.getPCZTDES());//计划周期描述
                    if (ruhujihuaEntityExt.getRhJhpl() != null) {
                        meterPlanModel.setAccountFrequency(ruhujihuaEntityExt.getRhJhpl().intValue());//计费频率  CFNAMEDES
                    }

                    meterPlanModel.setAccountDay(HzhrTypeUtil.string2integer(ruhujihuaEntityExt.getRhJhrq())); //计费日
                    meterPlanModel.setVeeCheckId(ruhujihuaEntityExt.getVeeId());
                    //vee校验描述
                    meterPlanModel.setVeeCheckDes(ruhujihuaEntityExt.getVEEDES()); //vee校验描述 VEEDES
                    meterPlanModels.add(meterPlanModel);
                }
                result.setModelList(meterPlanModels);
            }
        }

        result.setTotal(Total);
        return result;
    }


    //region javadoc注释 : post
    /**
     * 创建抄表计划 <br/><br/>
     * 1.新增状态为待执行的抄表计划； <br/>
     * 2.返回信息； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see MeterPlanPostModel body <br/>
     * @see MeterPlanGetModel result <br/>
     */ 
    //endregion
    //region 说明 : post
    //创建抄表计划
    //1.新增状态为待执行的抄表计划；
    //2.返回信息；
    //endregion
    //region 参数 : post
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    //endregion
    //region 请求 : post
    @RequestMapping(value = "/meter-plans", method = RequestMethod.POST)
    //endregion
    public MeterPlanGetModel post(Map<String, String> header,Map<String, Object> query, MeterPlanPostModel body) throws ServiceException{

        String _show = HzhrTypeUtil.object2string(query.get("_show"));
        Boolean _show_info = HzhrTypeUtil.object2boolean(query.get("_show_info"));

        UserCardModel userCard = getUserCard(header);
        String orgId = userCard.getUserCode().replaceFirst("010009", "00").replaceFirst("01", "00");
        //1,创建抄表计划
        Integer accountFrequency = body.getAccountFrequency();
        if (accountFrequency == null) {
            String planPeriodId = body.getPlanPeriodId();
            if ("1".equals(planPeriodId) || "2".equals(planPeriodId)) {
                accountFrequency = 2;
            }
            if ("3".equals(planPeriodId) || "21".equals(planPeriodId)) {
                accountFrequency = 1;
            }
            if ("31".equals(planPeriodId) || "32".equals(planPeriodId) || "33".equals(planPeriodId) ||"34".equals(planPeriodId) ||"35".equals(planPeriodId) ||"36".equals(planPeriodId) ||"37".equals(planPeriodId)) {
                accountFrequency = 7;
            }
            if ("41".equals(planPeriodId) || "42".equals(planPeriodId) || "43".equals(planPeriodId)) {
                accountFrequency = 10;
            }
        }

        RuhujihuaEntityExt ruhujihuaEntityExt = new RuhujihuaEntityExt();
        SimpleDateFormat df = new SimpleDateFormat("yyMMdd");//设置日期格式
        String date = df.format(new Date());
        int seq = ruhujihuaEntityCustomMapper.seq();
        String rhids_1 = date+ StrUtil.FillZeroForLeft("0",4)+seq;
        ruhujihuaEntityExt.setRhId(rhids_1);
        ruhujihuaEntityExt.setRhCbr(userCard.getUserName());//rhCbr
        SimpleDateFormat dfs = new SimpleDateFormat("yyyyMMdd");//RH_RQXZTJ 当天的格式化时间 格式YYYYMMDD
        String dates = dfs.format(new Date());
        //dateToDay
        ruhujihuaEntityExt.setRhRqxztj(dates);

        ruhujihuaEntityExt.setRhXiaciCbRq(HzhrDateUtil.getDayBegin());       // RH_XIACI_CB_RQ 当天零点
        ruhujihuaEntityExt.setVersion(HzhrTypeUtil.string2bigdecimal("1"));
        ruhujihuaEntityExt.setRhScl(0.00);
        ruhujihuaEntityExt.setRhCbndxs(HzhrTypeUtil.string2bigdecimal("0"));
        ruhujihuaEntityExt.setRhFjjhFlag("0");
        ruhujihuaEntityExt.setRhRhlxId(body.getTypeId());
        ruhujihuaEntityExt.setRhZhuangtai("2");  //待执行 状态(6705)
        ruhujihuaEntityExt.setCfId(body.getCollectFrequencyId());
        ruhujihuaEntityExt.setRhJhzq(body.getAccountPeriodId());
        ruhujihuaEntityExt.setRhYflx(body.getPlanPeriodId());
        ruhujihuaEntityExt.setRhJhpl(new BigDecimal(accountFrequency)); //body.getAccountFrequency()
        ruhujihuaEntityExt.setRhJhrq(body.getAccountDay().toString());
        ruhujihuaEntityExt.setVeeId(body.getVeeCheckId());//veeCheckId
        ruhujihuaEntityExt.setRhJhsm(body.getDes());
        ruhujihuaEntityExt.setOrgId(orgId);
        int ChangeCount = ruhujihuaEntityMapper.insertSelective(ruhujihuaEntityExt);
        String id =ruhujihuaEntityExt.getRhId();

        MeterPlanGetModel result = new MeterPlanGetModel();

        if (_show_info) {
            if ("one".equals(_show)) {
                RuhujihuaEntity ruhujihuaEntity_1 = ruhujihuaEntityMapper.selectByPrimaryKey(id);
                MeterPlanModel meterPlanModel = new MeterPlanModel();
                meterPlanModel.setId(ruhujihuaEntity_1.getRhId());
                meterPlanModel.setDes(HzhrTypeUtil.object2string(ruhujihuaEntity_1.getRhJhsm()));
                meterPlanModel.setTypeId(ruhujihuaEntity_1.getRhRhlxId());
                meterPlanModel.setTypeDes("");
                meterPlanModel.setStatusId(ruhujihuaEntity_1.getRhZhuangtai());
                meterPlanModel.setStatusDes("");
                meterPlanModel.setCollectFrequencyId(ruhujihuaEntity_1.getCfId()); //采集频率编号
                meterPlanModel.setCollectFrequencyDes("");  //getcollectFrequencyDes
                meterPlanModel.setAccountPeriodId(ruhujihuaEntity_1.getRhJhzq()); //计费周期编号  accountPeriodId
                meterPlanModel.setAccountPeriodDes("");
                meterPlanModel.setPlanPeriodId(ruhujihuaEntity_1.getRhYflx());  //计划周期编号
                meterPlanModel.setPlanPeriodDes("");
                meterPlanModel.setAccountFrequency(ruhujihuaEntity_1.getRhJhpl().intValue());  //计费频率
                meterPlanModel.setAccountDay(HzhrTypeUtil.string2integer(ruhujihuaEntity_1.getRhJhrq())); //计费日
                meterPlanModel.setVeeCheckId(ruhujihuaEntity_1.getVeeId());
                meterPlanModel.setVeeCheckDes("");
                result.setModel(meterPlanModel);
            }
            if ("list".equals(_show)) {
                List<RuhujihuaEntity> ruhujihuaEntity_2 = ruhujihuaEntityCustomMapper.selectByPrimaryKeyList(id);
                for (RuhujihuaEntity ruhujihuaEntity_3 : ruhujihuaEntity_2) {
                    List<MeterPlanModel> meterPlanModels = new ArrayList<>();
                    MeterPlanModel meterPlanModel = new MeterPlanModel();
                    meterPlanModel.setId(ruhujihuaEntity_3.getRhId());
                    meterPlanModel.setDes(HzhrTypeUtil.object2string(ruhujihuaEntity_3.getRhJhsm()));
                    meterPlanModel.setTypeId(ruhujihuaEntity_3.getRhRhlxId());
                    meterPlanModel.setTypeDes("");
                    meterPlanModel.setStatusId(ruhujihuaEntity_3.getRhZhuangtai());
                    meterPlanModel.setStatusDes("");
                    meterPlanModel.setCollectFrequencyId(ruhujihuaEntity_3.getCfId()); //采集频率编号
                    meterPlanModel.setCollectFrequencyDes("");  //getcollectFrequencyDes
                    meterPlanModel.setAccountPeriodId(ruhujihuaEntity_3.getRhJhzq()); //计费周期编号  accountPeriodId
                    meterPlanModel.setAccountPeriodDes("");
                    meterPlanModel.setPlanPeriodId(ruhujihuaEntity_3.getRhYflx());  //计划周期编号
                    meterPlanModel.setPlanPeriodDes("");
                    meterPlanModel.setAccountFrequency(ruhujihuaEntity_3.getRhJhpl().intValue());  //计费频率
                    meterPlanModel.setAccountDay(HzhrTypeUtil.string2integer(ruhujihuaEntity_3.getRhJhrq())); //计费日
                    meterPlanModel.setVeeCheckId(ruhujihuaEntity_3.getVeeId());
                    meterPlanModel.setVeeCheckDes("");
                    meterPlanModels.add(meterPlanModel);
                    result.setModelList(meterPlanModels);
                }
            }
        }
        result.setChangeCount(ChangeCount);
        return result;
    }



    //region javadoc注释 : put
    /**
     * 更新抄表计划 <br/><br/>
     * 1.更新抄表计划信息； <br/>
     * 2.返回信息； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param meterPlanId meterPlanId <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see MeterPlanPutModel body <br/>
     * @see MeterPlanGetModel result <br/>
     */ 
    //endregion
    //region 说明 : put
    //更新抄表计划
    //1.更新抄表计划信息；
    //2.返回信息；
    //endregion
    //region 参数 : put
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "meterPlanId", value = "meterPlanId",required = true, dataType = "string", paramType = "path"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    //endregion
    //region 请求 : put
    @RequestMapping(value = "/meter-plans/{meterPlanId}", method = RequestMethod.PUT)
    //endregion
    public MeterPlanGetModel put(Map<String, String> header,
                                 Map<String, Object> query,
                                 String meterPlanId,
                                 MeterPlanPutModel body) throws ServiceException{
        //抄表计划是否存在;否则报201
        RuhujihuaEntityExt ruhujihuaEntityExt_1 = ruhujihuaEntityCustomMapper.selectRuhujihuaAndmeterPlanId(meterPlanId);
        if (ruhujihuaEntityExt_1 == null){
            throw new ServiceException(Meterplan_ErrorsEnum.METERPLAN_METERGASUSERSERVICE_POST_201);
        }
        String _show = HzhrTypeUtil.object2string(query.get("_show"));
        Boolean _show_info = HzhrTypeUtil.object2boolean(query.get("_show_info"));

        UserCardModel userCard = getUserCard(header);
        String orgId = userCard.getUserCode().replaceFirst("01000901", "0000");
        ruhujihuaEntityExt_1.setRhJhsm(body.getDes());
        ruhujihuaEntityExt_1.setCfId(body.getCollectFrequencyId());
        ruhujihuaEntityExt_1.setRhJhzq(body.getAccountPeriodId());
        ruhujihuaEntityExt_1.setRhYflx(body.getPlanPeriodId());
        ruhujihuaEntityExt_1.setRhJhpl(HzhrTypeUtil.string2bigdecimal(body.getAccountFrequency()));
        ruhujihuaEntityExt_1.setRhJhrq(body.getAccountDay());
        ruhujihuaEntityExt_1.setVeeId(body.getVeeCheckId());
        ruhujihuaEntityExt_1.setOrgId(orgId);
        // ruhujihuaEntityExt_1.setRhZhuangtai("2");  //待执行 状态(6705) 抄表计划状态是不允许修改的
        int changeCount = ruhujihuaEntityMapper.updateByPrimaryKeySelective(ruhujihuaEntityExt_1);
        String id =ruhujihuaEntityExt_1.getRhId();
        MeterPlanGetModel result = new MeterPlanGetModel();
        if (_show_info) {
            if ("one".equals(_show)) {
                RuhujihuaEntity ruhujihuaEntity_1 = ruhujihuaEntityMapper.selectByPrimaryKey(id);
                MeterPlanModel meterPlanModel = new MeterPlanModel();
                meterPlanModel.setId(ruhujihuaEntity_1.getRhId());
                meterPlanModel.setDes(HzhrTypeUtil.object2string(ruhujihuaEntity_1.getRhJhsm()));
                meterPlanModel.setTypeId(ruhujihuaEntity_1.getRhRhlxId());
                meterPlanModel.setTypeDes("");
                meterPlanModel.setStatusId(ruhujihuaEntity_1.getRhZhuangtai());
                meterPlanModel.setStatusDes("");
                meterPlanModel.setCollectFrequencyId(ruhujihuaEntity_1.getCfId()); //采集频率编号
                meterPlanModel.setCollectFrequencyDes("");  //getcollectFrequencyDes
                meterPlanModel.setAccountPeriodId(ruhujihuaEntity_1.getRhJhzq()); //计费周期编号  accountPeriodId
                meterPlanModel.setAccountPeriodDes("");
                meterPlanModel.setPlanPeriodId(ruhujihuaEntity_1.getRhYflx());  //计划周期编号
                meterPlanModel.setPlanPeriodDes("");
                meterPlanModel.setAccountFrequency(ruhujihuaEntity_1.getRhJhpl().intValue());  //计费频率
                meterPlanModel.setAccountDay(HzhrTypeUtil.string2integer(ruhujihuaEntity_1.getRhJhrq())); //计费日
                meterPlanModel.setVeeCheckId(ruhujihuaEntity_1.getVeeId());
                meterPlanModel.setVeeCheckDes("");
                result.setModel(meterPlanModel);
            }
            if ("list".equals(_show)) {
                List<RuhujihuaEntity> ruhujihuaEntity_2 = ruhujihuaEntityCustomMapper.selectByPrimaryKeyList(id);
                for (RuhujihuaEntity ruhujihuaEntity_3 : ruhujihuaEntity_2) {
                    List<MeterPlanModel> meterPlanModels = new ArrayList<>();
                    MeterPlanModel meterPlanModel = new MeterPlanModel();
                    meterPlanModel.setId(ruhujihuaEntity_3.getRhId());
                    meterPlanModel.setDes(HzhrTypeUtil.object2string(ruhujihuaEntity_3.getRhJhsm()));
                    meterPlanModel.setTypeId(ruhujihuaEntity_3.getRhRhlxId());
                    meterPlanModel.setTypeDes("");
                    meterPlanModel.setStatusId(ruhujihuaEntity_3.getRhZhuangtai());
                    meterPlanModel.setStatusDes("");
                    meterPlanModel.setCollectFrequencyId(ruhujihuaEntity_3.getCfId()); //采集频率编号
                    meterPlanModel.setCollectFrequencyDes("");  //getcollectFrequencyDes
                    meterPlanModel.setAccountPeriodId(ruhujihuaEntity_3.getRhJhzq()); //计费周期编号  accountPeriodId
                    meterPlanModel.setAccountPeriodDes("");
                    meterPlanModel.setPlanPeriodId(ruhujihuaEntity_3.getRhYflx());  //计划周期编号
                    meterPlanModel.setPlanPeriodDes("");
                    meterPlanModel.setAccountFrequency(ruhujihuaEntity_3.getRhJhpl().intValue());  //计费频率
                    meterPlanModel.setAccountDay(HzhrTypeUtil.string2integer(ruhujihuaEntity_3.getRhJhrq())); //计费日
                    meterPlanModel.setVeeCheckId(ruhujihuaEntity_3.getVeeId());
                    meterPlanModel.setVeeCheckDes("");
                    meterPlanModels.add(meterPlanModel);
                    result.setModelList(meterPlanModels);
                }
            }
        }

        result.setChangeCount(changeCount);
        return result;
    }

}




